import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'home',
    component: () => import( '../views/homeView.vue')
  },
  {
    path: '/work',
    name: 'work',
    component: () => import( '../views/workView.vue'),
    // 独享路由守卫
    beforeEnter:(to,from,next)=>{
      let token = window.localStorage.getItem("userId");
      // 判断是否登录
      if(!token){
        next("/login");
        return;
      }else{
        next();
      }
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/loginView.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import( '../views/mainView.vue')
  },
  {
    path: '/my',
    name: 'my',
    component: () => import( '../views/myView.vue')
     
  },
  
]

// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err=>err);
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  },
})

export default router
